package edu.stanford.prpl.common.util;

import java.text.MessageFormat;
import java.util.LinkedList;
import java.util.List;

import edu.stanford.prpl.api.PRPLObject;

public class URIUtils {

	/*
	 * String u1 = "prpl://prpl-identity/#sseong@stanford.edu"; String u2 =
	 * "prpl://prpl-group/sseong@stanford.edu#group_name"; String u3 =
	 * "prpl://prpl-resource/sseong@stanford.edu#uuid"; String u4 =
	 * "prpl://prpl-device/sseong@stanford.edu#device_name";
	 * 
	 */

	private static final String IDENTITY_URI_PREFIX = "prpl://prpl-identity/";
	private static final String GROUP_URI_PREFIX = "prpl://prpl-group/";
	private static final String RESOURCE_URI_PREFIX = "prpl://prpl-resource/";
	private static final String DEVICE_URI_PREFIX = "prpl://prpl-device/";

	private static final String IDENTITY_URI_FORMAT = IDENTITY_URI_PREFIX + "#{0}";
	private static final String GROUP_URI_FORMAT = GROUP_URI_PREFIX + "{0}#{1}";
	private static final String RESOURCE_URI_FORMAT = RESOURCE_URI_PREFIX + "{0}#{1}";
	// private static final String RESOURCE_URI_FORMAT = RESOURCE_URI_PREFIX+"#{0}";
	private static final String DEVICE_URI_FORMAT = DEVICE_URI_PREFIX + "{0}#{1}";

	public static final String PRPL_SCHEME = "prpl";

	public static String getDeviceURI(String identityKey, String deviceName) {
		return MessageFormat.format(DEVICE_URI_FORMAT, identityKey, deviceName);
	}

	// public static String getResourceURI(String uuid) {
	// return MessageFormat.format(RESOURCE_URI_FORMAT, uuid);
	// }

	public static String getGroupURI(String identityKey, String groupName) {
		return MessageFormat.format(GROUP_URI_FORMAT, identityKey, groupName);
	}

	public static String getIdentityURI(String identityKey) {
		return MessageFormat.format(IDENTITY_URI_FORMAT, identityKey);
	}

	public static String getIdentityKey(String identityURI) {
		if (isIdentityURI(identityURI)) {
			return identityURI.replaceFirst(IDENTITY_URI_PREFIX + "#", "");
		}
		return null;
	}

	public static String getResourceURI(String identityKey, String uuid) {
		return MessageFormat.format(RESOURCE_URI_FORMAT, identityKey, uuid);
	}

	public static String getResourceUUID(String resourceURI) {
		return resourceURI.substring(resourceURI.lastIndexOf("#")+1);
	}
	
	public static String getDeviceName(String deviceURI) {
		if (isDeviceURI(deviceURI)) {
			deviceURI = deviceURI.replaceFirst(DEVICE_URI_PREFIX, "");
			deviceURI = deviceURI.substring(deviceURI.indexOf('#') + 1);
			return deviceURI;
		}
		return null;
	}

	public static boolean isDeviceURI(String URI) {
		if (URI == null)
			return false;
		if (URI.startsWith(DEVICE_URI_PREFIX))
			return true;
		else
			return false;
	}

	public static boolean isGroupURI(String URI) {
		if (URI == null)
			return false;
		if (URI.startsWith(GROUP_URI_PREFIX))
			return true;
		else
			return false;
	}

	public static boolean isIdentityURI(String URI) {
		if (URI == null)
			return false;
		if (URI.startsWith(IDENTITY_URI_PREFIX))
			return true;
		else
			return false;
	}

	public static boolean isResourceURI(String URI) {
		if (URI == null)
			return false;
		if (URI.startsWith(RESOURCE_URI_PREFIX))
			return true;
		else
			return false;
	}

	public static boolean isPRPLURI(String URI) {
		return isDeviceURI(URI) || isGroupURI(URI) || isIdentityURI(URI) || isResourceURI(URI);
	}

	public static List<String> getPRPLObjectURIs(List<? extends PRPLObject> prplObjects) {
		List<String> objectURIs = new LinkedList<String>();
		for (int i = 0; i < prplObjects.size(); i++) {
			if (prplObjects.get(i) instanceof PRPLObject) {
				PRPLObject prplObject = (PRPLObject) prplObjects.get(i);
				objectURIs.add(prplObject.getURI());
			}
		}
		return objectURIs;
	}

	public static String[] getPRPLObjectURIs(PRPLObject[] prplObjects) {
		String[] objectURIs = new String[prplObjects.length];
		for (int i = 0; i < objectURIs.length; i++) {
			objectURIs[i] = prplObjects[i].getURI();
		}
		return objectURIs;
	}

}
